package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportManageDepRelRpcService;
import com.hunttown.mes.rpc.domain.TransportManageDepRelDTO;
import com.hunttown.mes.rpc.domain.query.TransportManageDepRelDTOQuery;
import com.hunttown.mes.service.TransportManageDepRelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportManageDepRelConverts.convert;

/**
 * created by wangjunfu on 2022-01-28 11:16:11
 */
@Service
public class TransportManageDepRelRpcServiceImpl implements TransportManageDepRelRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportManageDepRelRpcServiceImpl.class);

    private final TransportManageDepRelService transportManageDepRelService;

    @Autowired
    public TransportManageDepRelRpcServiceImpl(TransportManageDepRelService transportManageDepRelService) {
        this.transportManageDepRelService = transportManageDepRelService;
    }

    @Override
    public TransportManageDepRelDTO insert(TransportManageDepRelDTO transportManageDepRelDTO) {
        try {
            return convert(transportManageDepRelService.insert(convert(transportManageDepRelDTO)));
        } catch (Exception e) {
            logger.error("TransportManageDepRel create failed.", e);
            throw new RuntimeException("TransportManageDepRel create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportManageDepRelDTO transportManageDepRelDTO) {
        try {
            return transportManageDepRelService.updateInfoById(convert(transportManageDepRelDTO));
        } catch (Exception e) {
            logger.error("TransportManageDepRel update by id failed.", e);
            throw new RuntimeException("TransportManageDepRel update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportManageDepRelDTOQuery transportManageDepRelDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportManageDepRelDTOQuery.getW_id() != null && transportManageDepRelDTOQuery.getW_id() > 0) {
                query.put("w_id", transportManageDepRelDTOQuery.getW_id());
            }

            if (transportManageDepRelDTOQuery.getIds() != null && !transportManageDepRelDTOQuery.getIds().equals("")) {
                query.put("ids", transportManageDepRelDTOQuery.getIds());
            }

            if (transportManageDepRelDTOQuery.getW_tableId() != null) {
                query.put("w_tableId", transportManageDepRelDTOQuery.getW_tableId());
            }

            if (transportManageDepRelDTOQuery.getW_recordId() != null) {
                query.put("w_recordId", transportManageDepRelDTOQuery.getW_recordId());
            }

            if (transportManageDepRelDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportManageDepRelDTOQuery.getDeleteFlag());
            }

            return transportManageDepRelService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportManageDepRel update by query failed.", e);
            throw new RuntimeException("TransportManageDepRel update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportManageDepRelDTO selectById(Integer id) {
        try {
            return convert(transportManageDepRelService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportManageDepRel select by id failed.", e);
            throw new RuntimeException("TransportManageDepRel select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportManageDepRelDTO selectObjByQuery(TransportManageDepRelDTOQuery transportManageDepRelDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportManageDepRelDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportManageDepRelDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(transportManageDepRelDTOQuery.getOrderBy())) {
                query.put("orderBy", transportManageDepRelDTOQuery.getOrderBy());
            }

            return convert(transportManageDepRelService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportManageDepRel select obj by query failed.", e);
            throw new RuntimeException("TransportManageDepRel select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportManageDepRelDTO> getListForPage(TransportManageDepRelDTOQuery transportManageDepRelDTOQuery) {
        Query query = Query.page(transportManageDepRelDTOQuery.getPn(), transportManageDepRelDTOQuery.getPageSize());

        if (transportManageDepRelDTOQuery.getId() != null && transportManageDepRelDTOQuery.getId() > 0) {
            query.put("id", transportManageDepRelDTOQuery.getId());
        }

        if (transportManageDepRelDTOQuery.getIds() != null && !transportManageDepRelDTOQuery.getIds().equals("")) {
            query.put("ids", transportManageDepRelDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportManageDepRelDTOQuery.getLikeName())) {
            query.put("likeName", transportManageDepRelDTOQuery.getLikeName());
        }

        if (transportManageDepRelDTOQuery.getTableId() != null) {
            query.put("tableId", transportManageDepRelDTOQuery.getTableId());
        }

        if (transportManageDepRelDTOQuery.getRecordId() != null) {
            query.put("recordId", transportManageDepRelDTOQuery.getRecordId());
        }

        if (transportManageDepRelDTOQuery.getDepId() != null) {
            query.put("depId", transportManageDepRelDTOQuery.getDepId());
        }

        if (StringUtils.isNotBlank(transportManageDepRelDTOQuery.getShowDate())) {
            query.put("showDate", transportManageDepRelDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportManageDepRelDTOQuery.getStartDate())) {
            query.put("startDate", transportManageDepRelDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportManageDepRelDTOQuery.getEndDate())) {
            query.put("endDate", transportManageDepRelDTOQuery.getEndDate());
        }

        if (transportManageDepRelDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportManageDepRelDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(transportManageDepRelDTOQuery.getOrderBy())) {
            query.put("orderBy", transportManageDepRelDTOQuery.getOrderBy());
        }

        List<TransportManageDepRelDTO> list = convert(transportManageDepRelService.selectByQuery(query));
        Page<TransportManageDepRelDTO> page = Page.from(transportManageDepRelDTOQuery.getPn(), transportManageDepRelDTOQuery.getPageSize(), list);
        page.setTotalItems(transportManageDepRelService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportManageDepRelService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportManageDepRel delete failed.", e);
            throw new RuntimeException("TransportManageDepRel delete failed, Please order your code.", e);
        }
    }
}